﻿@using NetSurveillance.WebApp.Utilities
@model NetSurveillance.WebApp.Models.DataRecordListModel
@{
    Layout = "~/Views/Shared/_SimpleLayout.cshtml";
    var supportFields = ViewBag.SupportFields;
    var displayFields = ViewBag.DisplayFields;
}
@section javascript
{
    <script type="text/javascript">
        $(function() {
            $('#middle_right_prepage').click(goPrePage);
            $('#middle_right_nextpage').click(goNextPage);
            $('#middle_right_goto').click(goPage);
            $('#chk_checkall').change(checkAll);
            $('#middle_right_showall').click(displayAll);
            $('#middle_right_delete').click(deleteRecords);
            $('#middle_right_select').click(function () {
                var searchId = $('#MultiSearchId').val();
                $.openModalDialog({
                    url: '@Url.Action("Search","Search")' + '?searchFrom=history&searchId=' + searchId,
                        width: '950px',
                        height: '610px',
                        scroll: false
                    });
            });
            $('#middle_right_export').click(exportConfirm);
        });

        function goPrePage() {
            var pageIndex = parseInt($('#hidPageIndex').val());
            return goPage(pageIndex - 1);
        }

        function goNextPage() {
            var pageIndex = parseInt($('#hidPageIndex').val());
            return goPage(pageIndex + 1);
        }

        function goPage(goPageIndex) {
            if (typeof goPageIndex != 'number' && $('#txt_page').val() == '') {
                alert('请输入需要跳转的页数');
                return false;
            }
            $.showLoading('正在加载数据');
            if (typeof goPageIndex == 'number')
                $('#txt_page').val(goPageIndex);
            document.forms[0].submit();
            return false;
        }

        function checkAll() {
            var isCheckAll = $('#chk_checkall').is(':checked');
            $('#tbRecords tbody td:first-child :checkbox').attr('checked', isCheckAll);
        }

        function displayAll() {
            var $hidDisplayAll = $('#DisplayAll');
            $hidDisplayAll.val($hidDisplayAll.val() == 'False' || $hidDisplayAll.val() == 'false');
            return goPage();
        }

        function deleteRecords() {
            var $checkedEle = $('#tbRecords tbody :checkbox:checked');
            if ($checkedEle.size() == 0) {
                alert('请勾选要删除的记录');
                return false;
            }
            if (!window.confirm('@Resources.MessageResource.Delete_Confirm')) return false;
            var checkedIds = $.map($checkedEle, function(ele) {
                return parseFloat($(ele).val()) || -1;
            });
            $.ajax({
                url: '@Url.Action("DeleteRecords", "Task")',
                type: 'post',
                dataType: 'json',
                traditional: true,
                data: { dataRecordIds: checkedIds },
                success: function(result) {
                    if (result.success) {
                        window.location.replace(window.location.href);
                        return;
                    }
                    alert(result.message);
                }
            });
            return false;
        }

        function sortOrder(orderField, order) {
            var $hidOrderField = $('#OrderField'),
                $hidOrder = $('#Order');
            $hidOrderField.val(orderField);
            $hidOrder.val(order);
            return goPage();
        }
        
        function exportConfirm() {
            var $checkedEle = $('#tbRecords tbody :checkbox:checked');
            if ($checkedEle.size() == 0) {
                alert('请勾选要导出的记录');
                return false;
            }
            var checkedIds = $.map($checkedEle, function (ele) {
                return parseFloat($(ele).val()) || -1;
            });
            $('#exportConfirmContain').dialog({
                title: '导出确认',
                width: 300,
                height: 150,
                closeOnEscape: false,
                draggable: false,
                resizable: false,
                modal: true,
                autoOpen: true,
                open: function () {
                    $(this).parent('.ui-dialog').find('.ui-dialog-titlebar-close').hide();
                },
                buttons: {
                    ' 是 ': function () {
                        exportRecords(checkedIds, true);
                    },
                    ' 否 ': function () {
                        exportRecords(checkedIds, false);
                    },
                    ' 取消 ': function () {
                        $(this).dialog('close');
                    }
                }
            });
            return false;
        }

        function exportRecords(ids, includeFile) {
            window.location.href =
                '@Url.Action("ExportRecords", "Task")?' + $.param({ drIds: ids, isIncludeFile: includeFile }, true);
            $('#exportConfirmContain').dialog('close');
        }
    </script>
}
@section css
{
    <style type="text/css">
        #tbRecords tbody tr td
        {
            min-width: 80px;
            border: .5px solid gray;
            max-width: 180px;
            overflow: hidden;
            -ms-word-break: keep-all;
            word-break: keep-all;
            white-space: nowrap;
            -ms-text-overflow: ellipsis;
            -o-text-overflow: ellipsis;
            text-overflow: ellipsis;
        }
        #tbRecords tbody tr td.Title,#tbRecords tbody tr td.Url
        {
            max-width: 320px;
        }
    </style>
}
@using (Html.BeginForm())
{
    <div class="middle_right_title" id="middle_right_top">
        <div id="middle_right_select"><a href="javascript:;">查询</a></div>
        <div id="middle_right_delete" style="visibility: @((ViewBag.IsHideDelete??true) ? "hidden" : "")"><a href="javascript:;">删除</a></div>
        <div id="middle_right_export"><a href="javascript:;">导出</a></div>
    </div>
    <div id="middle_right_body">
        @Html.ValidationSummary()
        <table id="tbRecords" class="middle_list_tab">
            <thead>
                <tr>
                    <th></th>
                    @foreach (var supportField in supportFields)
                    {
                        if (ViewUtility.InDisplayFields(displayFields, supportField.PropertyName))
                        {
                            string order = Model != null && supportField.PropertyName == Model.OrderField ?
                                               (Model.Order == "asc" ? "desc" : "asc") : "desc";
                            <th>
                                <a href="javascript:;"
                                   onclick=" sortOrder('@supportField.PropertyName', '@order') "
                                   title="@supportField.DisplayText">
                                    @supportField.DisplayText
                                </a>
                                @if (Model != null && supportField.PropertyName == Model.OrderField)
                                {
                                    <div class="@(order == "asc" ? "" : "tabAsc")" title="@(order == "asc" ? "降序" : "升序")">
                                        &nbsp;&nbsp;&nbsp;&nbsp;
                                    </div>
                                }
                            </th>
                        }
                    }
                </tr>
            </thead>
            <tbody>
                @Html.Partial("../Task/_DataRecords", Model == null ? null : Model.DataRecords)
            </tbody>
        </table>
    </div>
    <div class="middle_right_title" id="middle_right_bottom">
        <div class="middle_right_bottom_font">
            <label><input type="checkbox" id="chk_checkall" />&nbsp;全选</label>
        </div>
        <div class="middle_right_bottom_font">
            结果 &nbsp;&nbsp; @(Model != null ? Model.CurrentCount : "0")/@(Model != null ? Model.TotalCount : 0) &nbsp;
        </div>
        <div id="middle_right_goto" style="visibility: @(Model != null && !Model.DisplayAll ? "" : "hidden")">
            <a href="javascript:;">转到</a>
        </div>
        <div class="middle_right_font" style="visibility: @(Model != null && !Model.DisplayAll ? "" : "hidden")">
            第&nbsp;
            @Html.TextBoxFor(m => m.PageIndex, new {id = "txt_page"})
            &nbsp;页
        </div>
        <div id="middle_right_showall">
            <a href="javascript:;">
                @(Model != null && Model.DisplayAll ? "分页显示" : "显示全部")
            </a>
        </div>
        <div id="middle_right_prepage" style="visibility: @(Model != null && !Model.DisplayAll && Model.PageIndex != 1 ? "" : "hidden")">
            <a href="javascript:;">上一页</a>
        </div>
        <div id="middle_right_nextpage" style="visibility: @(Model != null && !Model.DisplayAll && !Model.IsLastPage ? "" : "hidden")">
            <a href="javascript:;">下一页</a>
        </div>
    </div>
    @Html.HiddenFor(m => m.DisplayAll)
    @Html.HiddenFor(m => m.OrderField)
    @Html.HiddenFor(m => m.Order)
    @Html.Hidden("hidPageIndex", Model == null ? 0 : Model.PageIndex)
    @Html.Hidden("hidMaxRecordId", Model == null || Model.DataRecords == null || !Model.DataRecords.Any() ? 0 : Model.DataRecords.Max(p => p.Id))
    @Html.Hidden("MultiSearchId")

    <div id="exportConfirmContain" class="ui-helper-hidden">@Resources.MessageResource.ExportRecords_Confirm</div>
}